package com.baobao.utility;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonCalendarValueProcessor implements JsonValueProcessor {
	private String datePattern = "yyyy-MM-dd HH:mm:ss";

	public JsonCalendarValueProcessor() {
		super();
	}

	// 构造函数
	public JsonCalendarValueProcessor(String format) {
		super();
		this.datePattern = format;
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		// TODO Auto-generated method stub
		return process(value);
	}

	public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
		// TODO Auto-generated method stub
		return process(value);
	}

	private Object process(Object value) {
		try {
			if (value instanceof Calendar) {
				SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
				return sdf.format(((Calendar) value).getTime());
			}
			return value == null ? "" : value.toString();
		} 
		catch (Exception e) {
			return "";
		}
	}
}
